package com.jbl.te.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.ma.vo.TeacherUserVO;
import com.jbl.te.entity.College;
import com.jbl.te.entity.Course;
import com.jbl.te.entity.Teacher;
import com.jbl.te.entity.dto.TeacherQuery;
import com.jbl.te.entity.vo.TeacherVO;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.DepartmentService;
import com.jbl.te.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.jbl.commonutils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/te/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CollegeService collegeService;

    @GetMapping("/getAllTeacher")
    public R getAllTeacher() {
        return R.ok().data("list",teacherService.list());
    }

    @DeleteMapping("delTeachersByList")
    @ApiOperation("批量删除教师")
    public R delTeachersByList(@RequestBody  List<String> idList) {
        boolean b = teacherService.removeByIds(idList);
        if(b) {return R.ok();}
        return R.error().message("批量删除失败");
    }


    @PostMapping("{page}/{limit}")
    @ApiOperation("分页教师列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam,teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        List<TeacherVO> list = new ArrayList<>();
        for (Teacher teacher : records) {
            TeacherVO vo = new TeacherVO();
            BeanUtils.copyProperties(teacher,vo);
            College college = collegeService.getById(teacher.getCollegeId());
            vo.setCollegeName(college.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }


    @GetMapping("getTeacherById")
    @ApiOperation("根据id查找教师")
    public R getTeacherById(String id) {
        Teacher teacher = teacherService.getById(id);
        if(teacher == null) {
            return R.error().message("教师不存在");
        }
        TeacherVO vo = new TeacherVO();
        BeanUtils.copyProperties(teacher,vo);
        College college = collegeService.getById(teacher.getCollegeId());
        vo.setCollegeName(college.getName());
        return R.ok().data("teacher",vo);
    }

    @PutMapping("updateTeacher")
    @ApiOperation("修改教师")
    public R updateTeacher(@RequestBody Teacher teacher) {
        boolean b = teacherService.updateTeacher(teacher);
        if(b) {
            return ok();
        } else {
            return R.error().message("修改失败");
        }
    }

    @DeleteMapping("delTeacherById")
    @ApiOperation("删除教师")
    public R delTeacherById(String id) {
        boolean b = teacherService.removeTeacherById(id);
        if(b) {
            return ok();
        } else {
            return R.error().message("删除失败");
        }
    }


    @PostMapping("saveTeacher")
    public R saveTeacher(@RequestBody TeacherUserVO teacher) {

        String teacherNumber = teacherService.saveTeacher(teacher);
        if(!StringUtils.isEmpty(teacherNumber)) {
            return R.ok().data("teacherNumber",teacherNumber);
        } else {
            return R.error().message("添加失败");
        }
    }

    @GetMapping("/getTeacherByName")
    public R getTeacherByName(String name) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.select("id","name");
        wrapper.likeRight("name",name);
        return R.ok().data("list",teacherService.list(wrapper));
    }

    @GetMapping("/checkTeacherByName")
    public R checkTeacherByName(String name) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        Teacher teacher = teacherService.getOne(wrapper);
        Boolean b = teacher!=null;
        if(!b) return R.ok().data("flag",b);
        else return R.ok().data("flag",b).data("id",teacher.getId());
    }

}

